<?php
namespace App\Models\Promotion;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\Redis;

class PromotionOrderGiftGoods extends Model
{

    protected $table = 'promotion_order_gift_goods';

    protected $primaryKey = 'id';

    public $timestamps = false;

    public $incrementing = false;

    public static function getCache( $id ) {

        $goods = Redis::get('G_WDH_BILL_PROMOTION_GOODS_' . $id );

        if ( empty( $goods ) ) {

            $goods_data = self::where('id',$id)->get()->toArray();
            if ( empty( $goods_data ) ) {
                return null;
            }

            foreach ( $goods_data as $item ) {
                $goods[] = $item['postID'];
            }

            Redis::setex( 'G_WDH_BILL_PROMOTION_GOODS_' . $id , 604800 , json_encode( $goods ));

        } else {
            $goods = json_decode( $goods , true );
        }

        return $goods;

    }
    
}
